package com.mmall.concurrency.example.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by RID on 2018/4/13.
 */
@Slf4j
public class CyclicBarrierExample {
    private static CyclicBarrier barrier= new CyclicBarrier(5,()->{log.info("callback is running");});
    public static void main(String[] args) throws Exception{
        ExecutorService executor= Executors.newCachedThreadPool();

        for (int i=0;i<10; i++){
            final int tnum=i;
            Thread.sleep(1000);
            executor.execute(()->{
                try {
                    race(tnum);
                } catch (Exception e) {
                    log.error("ex",e);
                }
            });
        }

    executor.shutdown();
    }
    private static void race(int threadnum) throws Exception{
         Thread.sleep(1000);
         log.info("{} is ready",threadnum);
         barrier.await();
         log.info("{} continue",threadnum);


    }

}
